package com.edgar.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RedirectOrDispatcherServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Enumeration enums = req.getParameterNames();
        while (enums.hasMoreElements()) {
            String key = (String) enums.nextElement();
            System.out.println(req.getParameter(key));
        }
        String type = req.getParameter("type");
        if ("redirect".equals(type)) {
            resp.sendRedirect(req.getContextPath()
                    + "/servlet/DemoServlet.do");
        } else if ("dispatch".equals(type)) {
            req.getRequestDispatcher(
            "/servlet/DemoServlet.do").forward(req, resp);
        } else {
            PrintWriter out = resp.getWriter();
            out.println("success");
        }
    }

}
